<template>
    <common>
        <div class="ve_container">
            <el-card :body-style="{ background: 'rgba(0,0,0,0.15)' }">
                <el-button class="ve_submit" type="plain" style="float: right">
                    <router-link
                        :to="{ path: 'register' }"
                        style="text-decoration: none; color: inherit"
                        >注册</router-link
                    >
                </el-button>
                <h1>懒人生态</h1>
                <transition name="el-fade-in-linear">
                    <el-form
                        :model="form"
                        :rules="rules"
                        v-show="!success"
                        class="ve_form"
                        ref="ref_form"
                        :inline="false"
                        @keyup.enter="onSubmit"
                    >
                        <el-form-item prop="username">
                            <el-input
                                v-model.trim="username"
                                placeholder="用户名"
                            >
                                <template #prepend>
                                    <el-icon :size="20"><Avatar /></el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input
                                v-model.trim="password"
                                show-password
                                placeholder="密码"
                            >
                                <template #prepend>
                                    <el-icon :size="20"><Key /></el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button
                                class="ve_submit"
                                type="primary"
                                @click="onSubmit"
                            >
                                登录
                            </el-button>
                        </el-form-item>
                    </el-form>
                </transition>
            </el-card>
        </div>
    </common>
</template>

<script setup>
import {
    SET_ACCESS_TOKEN,
    SET_REFRESH_TOKEN,
    SET_TOKEN,
    SET_UNAME,
    SET_USER_ID,
} from "@/store/modules/app/type";
import Common from "@/components/Common";
import { ref, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const rules = {
    username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};
const store = useStore();
const router = useRouter();
const form = reactive({
    username: "",
    password: "",
});
const { username, password } = toRefs(form);
const ref_form = ref(null);
const success = ref(false);
sessionStorage.clear();
store.dispatch(`app/${SET_TOKEN}`, "");
router.options.isAddDynamicMenuRoutes = false;

/**
 * 登录
 */
const onSubmit = () => {
    ref_form.value.validate(async (valid) => {
        if (valid) {
            try {
                const data = await VE_API.system.login(form);
                if (data.code === 0) {
                    await store.dispatch(`app/${SET_TOKEN}`, data.data);
                    await store.dispatch(
                        `app/${SET_ACCESS_TOKEN}`,
                        data.data.accessToken,
                    );
                    await store.dispatch(
                        `app/${SET_REFRESH_TOKEN}`,
                        data.data.refreshToken,
                    );
                    await store.dispatch(`app/${SET_UNAME}`, form.username);

                    // 获取用户信息
                    try {
                        const userResult = await VE_API.system.resolvingUser({
                            accessToken: data.data.accessToken,
                        });

                        if (userResult && userResult.id) {
                            await store.dispatch(
                                `app/${SET_USER_ID}`,
                                userResult.id,
                            );
                        }
                    } catch (userError) {
                        console.warn("获取用户信息失败:", userError);
                        // 用户信息获取失败不影响登录流程
                    }

                    success.value = true;
                    ElMessage.success("登录成功");
                    router.push({ name: "Zone" });
                } else {
                    // 显示业务错误信息
                    const errorMsg =
                        data.message || "登录失败，请检查用户名和密码";
                    ElMessage.error(errorMsg);
                }
            } catch (error) {
                // 网络错误等异常情况已经在axios拦截器中处理
                console.error("登录异常:", error);
            }
        } else {
            return;
        }
    });
};
</script>

<style lang="scss" scoped>
.ve_container {
    position: absolute;
    z-index: 1;
    width: 400px;
    top: 50%;
    left: 100px;
    transform: translateY(-50%);
    transition: all 1s;
    min-height: 273px;
    text-align: center;
    h1 {
        font-size: 24px;
        transition: all 1s;
        font-weight: bold;
        margin-bottom: 36px;
    }
    .ve_form {
        .ve_submit {
            width: 100%;
        }
        :deep(.el-input-group__prepend) {
            padding: 0 10px;
        }
    }
}
.el-form-item {
    width: 100%;
}
</style>
